#!/bin/sh

# make the daily SimGear snapshot

ROOT="${HOME}/Projects/SimGear-0.3"
CHANGEROOT="${HOME}/Projects/FlightGear-0.7"
SNAPSHOT_DIR="${HOME}/ftp_simgear/Devel/Snapshots"
SAVE_COUNT=8

# roll up the snap shot and keep track of the file name
cd $ROOT/SimGear
echo "running make dist in ${ROOT}/SimGear"
LINE=`make dist | grep GZIP`
FILE=`echo $LINE | awk '{ print $4 }'`

# copy the snap shot over to the snapshot directory
echo "cp ${ROOT}/SimGear/${FILE} ${SNAPSHOT_DIR}"
cp ${ROOT}/SimGear/${FILE} ${SNAPSHOT_DIR}

# go to the snap shot directory and untar the image
builtin cd ${SNAPSHOT_DIR} || ( echo "can't cd to ${SNAPSHOT_DIR}!!!"; exit )
BASE=`basename ${FILE} .tar.gz`
echo $BASE
echo rm -rf ${BASE}.old
rm -rf ${BASE}.old
echo mv ${BASE} ${BASE}.old
mv ${BASE} ${BASE}.old
tar xzf ${FILE}

# delete all but the newest $SAVE_COUNT snapshots
echo "rm -f /tmp/dummy.9876543210 \
	`ls -t ${SNAPSHOT_DIR}/*.gz | sed -e "1,${SAVE_COUNT}d"`"
rm -f /tmp/dummy.9876543210 \
	`ls -t ${SNAPSHOT_DIR}/*.gz | sed -e "1,${SAVE_COUNT}d"`

# now generate the current changelog
DIR=`tar tzvf ${FILE} | head -1 | awk '{ print $6 }'`
echo ${CHANGEROOT}/admin/gen-change-log.pl ${SNAPSHOT_DIR}/${DIR} ${SNAPSHOT_DIR}/changelog-0.3.mini
${CHANGEROOT}/admin/gen-change-log.pl ${SNAPSHOT_DIR}/${DIR} ${SNAPSHOT_DIR}/changelog-0.3.mini
